const contentView = () => import("@layout/content.vue");
const layoutView = () => import("@layout/layout.vue");
import store from "@store/index";
// 获取所有子级
function getchildren(obj, parent) {
    let childrenlist = obj.children.map((ele) => {
      return ele.path;
    });
    if(parent){
      childrenlist.push(...parent)
    }
    for (let index = 0; index < obj.children.length; index++) {
      const element = obj.children[index];
      if(element.children){
        return getchildren(element,childrenlist);
      }else{
        return childrenlist.reverse()
      }
  }
}

//核心处理方法
function createRoute(route, parent) {
  for (let index = 0; index < route.length; index++) {
    const item = route[index];
    if (!item) {
      return;
    }
    item.meta = {};
    item.meta.title = item.title;
    item.meta.click = true;
    // 子级加父元素
    if (parent) {
      item.parent = parent;
    } else {
      item.parent = [];
    }
    // 判断是否需要左侧菜单
    // 判断是否有子级
    if (item.children) {
      item.component = layoutView;
      item.meta.click = false;
      // 父级加子级
      item.childrenpath = getchildren(item)
      createRoute(item.children, [...item.parent, item.path]);
    } else {
      if (!item.component) {
        item.component = contentView;
        return;
      }
      let componentFn = import(`../view/${item.component}.vue`);
      item.component = () => componentFn;
    }
  }
  return route;
}

export const dynamic = () => {
  const { getAsyncRoutes } = store.getters;
  return createRoute(getAsyncRoutes);
};
